// pages/home/device.js
var app = getApp();

var page_did = "";
var page_type = "";
var page_index = 0;
var page_size = 20;
var page_refresh = false;
var page_timer = null;
var page_total = 20;
var query_size = 20;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    device: null,
    set_enable: true,
    alarm: [],
    history: [],
    currentcount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    page_timer = null;
    //console.log(options);
    page_did = options.did;
    page_type = options.type;
    //console.log("page_did:" + page_did)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var token = app.globalData.Token;//app.globalData.Token;
    if (token == null || token == "") {
      wx.navigateTo({
        url: '/pages/user/login'
      })
      return;
    }
    page_timer = null;
    page_refresh = true;
    this.getdevice("page_type:"+page_type);
    console.log();
    if (page_type == "NH301" || page_type == "LUX01" || page_type == "SHT3X" || page_type == "BH1750" || page_type == "VD01") {
      this.getalarm();
    }
    else if (page_type == "ACS01" || page_type == "ACS02") {
      this.gethistory();
    }
    this.ontimer();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
  },
  //获取设备数据
  getdevice: function (e) {
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    //console.log("getdevice");
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;

    wx.request({
      url: app.globalData.Url + "Api/Device/GetDevice", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        that.setData({ device: res.data.data });
        var snap = res.data.data.Snap;
        for (var i in snap) {
          if (snap[i].V == "V1") {
            that.setData({ set_enable: snap[i].Value });
            return;
          }
        }
      }
    })
  },
  //获取设备报警
  getalarm: function (e) {
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    //console.log("getalarm");
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/GetAlarm", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
        start: null,
        end: null,
        index: page_index,
        size: query_size,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        page_total = res.data.data.Data.length;
        that.setData({ alarm: res.data.data });
      }
    })
  },
  //获取设备报警
  gethistory: function (e) {
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/GetHistory", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
        cmd: 'Device/History',
        start: null,
        end: null,
        index: page_index,
        size: query_size,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        page_total = res.data.data.length;
        that.setData({ history: res.data.data });
        that.setData({ currentcount: res.data.ext });
      }
    })
  },
  //定时方法
  ontimer: function () {
    var that = this;
    page_timer = setTimeout(function () {
      //console.log("----ontimer 定时器...----");
      if (page_refresh) {
        that.getdevice();
        if (page_type == "NH301" || page_type == "LUX01" || page_type == "SHT3X" || page_type == "BH1750" || page_type == "VD01")
        {
          that.getalarm();
        }
        else if (page_type == "ACS01" || page_type == "ACS02")
        {
          that.gethistory();
        }
        that.ontimer();
      }
    }, 10000);
  },
  //设置
  onsetting: function (e) {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
    wx.navigateTo({
      url: '/pages/home/device/setting?did=' + page_did + '&name=' + this.data.device.Info.Name
    })
  },
  //解绑设备
  unbind: function () {
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/UnBind", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        wx.reLaunch({
          url: '/pages/home/index'
        })
      }
    })
  },
  //解绑设备事件
  onunbind: function (e) {
    var that = this;
    wx.showModal({
      title: '解绑',
      content: '确定解绑该设备?',
      success: function (res) {
        if (res.confirm) {
          that.unbind();
        }
      }
    })
  },
  //qrcode
  onqrcode: function (e) {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
    wx.navigateTo({
      url: '/pages/home/qr?did=' + page_did
    })
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    if (page_total >= query_size) {
      query_size = query_size + page_size;
    }
   
    if (page_type == "NH301" || page_type == "LUX01" || page_type == "SHT3X" || page_type == "BH1750" || page_type == "VD01") {
      this.getalarm();
    }
    else if (page_type == "ACS01" || page_type == "ACS02") {
      this.gethistory();
    }
  },
  //onchart
  onchart: function (e) {
    clearTimeout(page_timer);
    page_refresh = false;
    page_timer = null;
    wx.navigateTo({
      url: '/pages/home/device/chart?did=' + page_did + '&name=' + this.data.device.Info.Name + "&cmd=0"
    })
  },
})